$(function () {
    //获取数据
    chageTabsFn(2, false);
    //兑换人民币
    toExchangeRmbFn();
    $("body").on("click","#integral-cm-tabs > .wm-cm-tabs",function(){
        $(this).addClass("active").siblings().removeClass("active");
        var index = $(this).data("value")
        $('.i-media-list > .intergral-list-box').hide();
        $('.intergral-list-' + index).show();
        chageTabsFn(index, true)
    }).on("click","#prompt",function(){
        var html ="<div class='fs6'><p align='left'>" +
            "1、部分场景获得的"+userIntegralRuleName+"拥有保护期，保护期内的"+userIntegralRuleName+"不可使用。</p>" +
            "<p align='left'>2、当前平台"+userIntegralRuleName+"保护期为："+protectDay+"天。</p>" +
            "<p align='left'>3、当前你的保护期内"+userIntegralRuleName+"为："+protectScore+"个。</p>"

        if(validTime.type=="2"){
            html+= "<p align='left'>4、每年的"+validTime.month+"月"+validTime.day+"日清空"+userIntegralRuleName+"，请尽快使用</p>"
        }
        html+= "</div>";
        Zepto.alert(html,null,function () {

        });
        Zepto('.modal-in').find('.modal-button').text('知道了');
    }).on("click","#invitation",function(){
        var desc = $(this).data("desc");
        posters(desc,baseloaction+"/mobile/user/register?invitationKey="+SHARE_KEY,'','',2);
    });

    function chageTabsFn(rType, changeTabFlag) {
        var iContent = $(".intergral-list-box"),
            timer = null;

        var loading = '<section class="loading-gif__wrap"><div class="row"><aside class="col-auto"><span class="loadingImg"></span></aside><aside class="col-auto"><span class="loading-txt"><big>努力奔跑中...</big><br>';
        loading += '</span></aside></div></section>';

        iContent.empty();
        $(".infinite-scroll-preloader").remove();
        $('.intergral-list-' + rType).append(loading);
        _setSessionStorage('cTabFlag', changeTabFlag);
        _setSessionStorage('rType', rType);
        clearTimeout(timer);
        timer = setTimeout(function () {
            $(".loading-gif__wrap").remove();
            listIntegralAjaxe(1, rType);
        }, 600);
    }

    function listIntegralAjaxe(pageNo, rType) {
        var params = {
            'recordType': rType
        };
        params = JSON.stringify(params);
        loadMobilePathData({
            url: baseloaction + '/mobile/uc/integral/integralAjax',
            contextId: 'integral-list-' + rType,
            pageNo: pageNo,
            pageCall: listIntegralAjaxe,
            params: params,
            callback: function (pageNo) {
            }
        });
    }

});
//兑换人民币

function toExchangeRmbFn() {
    var _ele = $('#toExchangeRmbEle').val();
    $(document).on('input', "input[name='cash']", function () {
        var value =$(this).val();
        if(parseInt(value)>parseInt(currentScore)){
            $(this).val(currentScore)
            value =currentScore;
            Zepto.toast("不能超过"+currentScore);
        }
        if(parseInt(value)<parseInt(integral)){
            return;
        }else{
            $($(".toExchangeRmbEle").find("#money")[0]).text((money/integral*value).toFixed(1))
        }
    });

    $(document).on('click', '#toExchangeRmb', function () {
        $.confirm(_ele, '',
            function (em) {
                var cash =    em.find("input[name='cash']").val()
                if(cash==""||cash==0){
                    Zepto.toast("请填写兑换的积分");
                    return;
                }
                if(parseInt(cash)>parseInt(currentScore)){
                    Zepto.toast("不能超过最大可用积分");
                    return;
                }

                $.ajax({
                    url: baseloaction + "/mobile/uc/integral/redeemedCash",
                    type: "post",
                    data:{"cash":cash},
                    dataType: "json",
                    success: function (result) {
                        Zepto.toast( result.message);
                        setTimeout(function(){
                            location.reload();
                        },400)
                    },
                    error: function (error) {
                        Zepto.toast("系统繁忙,请稍后重试");
                    }
                })

            }
        );
        $('.modal-in').find('.modal-button').css('color', '#bbb');
        $('.modal-in').find('.modal-button-bold').text('确定兑换').attr('style', '');
    })

}

function goBack(){
    var  noMobile  = getCookie("noMobile");
    if(noMobile!=null){
        if (noMobile == "1" ){
            android.goBack();
        }
        if( noMobile == "2") {
            window.webkit.messageHandlers.goBack.postMessage("");
        }
        return true;
    }
    window.location.href=baseloaction+"/mobile/uc/index"
}